#################
## build stage ##
#################
FROM hub.tangjiabin.cn/rust:1.82.0-slim-bookworm AS builder
WORKDIR /code

ENV RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
ENV RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup
ENV SWAGGER_UI_DOWNLOAD_URL=file:/code/swagger/swagger-ui-5.20.1.zip
# 安装 OpenSSL 和 pkg-config
RUN apt-get update && apt-get install -y libssl-dev pkg-config curl && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN USER=root cargo init
COPY Cargo.toml Cargo.toml 
RUN cargo fetch

# 复制源代码
COPY src src
COPY swagger /code/swagger

# 构建依赖
RUN cargo build --release

###############
## run stage ##
###############
FROM hub.tangjiabin.cn/bitnami/minideb:bookworm
WORKDIR /app

# 配置软件源
ENV RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
ENV RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup

# 安装 OpenSSL 和 pkg-config
RUN apt-get update && apt-get install -y libssl-dev pkg-config curl && apt-get clean && rm -rf /var/lib/apt/lists/*

# 复制构建好的二进制文件
COPY --from=builder /code/target/release/land-dev land-dev

# 暴露应用运行的端口
EXPOSE 9000

# 运行应用，使用环境变量中的端口
CMD ["/app/land-dev"]